<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图表名称" prop="echartName">
        <el-input
          v-model="queryParams.echartName"
          placeholder="请输入图表名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图表类别" prop="echartType">
        <el-select v-model="queryParams.echartType" placeholder="请选择图表类别" clearable>
          <el-option
            v-for="dict in dict.type.sys_echart_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-radio-group v-model="isType" style="float: right;" size="mini">
        <el-radio-button :label="false">卡片</el-radio-button>
        <el-radio-button :label="true">列表</el-radio-button>
      </el-radio-group>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ghxx:bEchartType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ghxx:bEchartType:edit']"
          v-if="isAuth"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ghxx:bEchartType:remove']"
          v-if="isAuth"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bEchartTypeList" @selection-change="handleSelectionChange" v-if="isType">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图表名称" align="center" prop="echartName" />
      <el-table-column label="图表类别" align="center" prop="echartType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_echart_type" :value="scope.row.echartType"/>
        </template>
      </el-table-column>
      <el-table-column label="图表图片" align="center" prop="echartImgae" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.echartImgae" :width="40" :height="0"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="jumpDetail(scope.row)"
            v-hasPermi="['ghxx:bEchartType:edit']"
            v-if="queryAuth(scope.row.deptIdStrs)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ghxx:bEchartType:remove']"
            v-if="queryAuth(scope.row.deptIdStrs)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row v-loading="loading" v-else>
      <el-col :span="cardSpan" v-for="item in bEchartTypeList" :key="item.id" style="padding: 2px;">
        <div style="width:auto;height:300px;">
          <el-card :body-style="{ padding: '5px'}" style="height: 100%;">
            <span class="device-name">{{ item.echartType }}</span>
            <div @click="jumpDetail(item)">
              <el-image :src="item.echartImgae == null ? noImage : baseApi + item.echartImgae" title="编辑" class="ztImage"
              />
            </div>
            <div style="padding: 5px;">
              <span>{{ item.echartName }}</span>
              <div class="bottom clearfix">
                <el-button type="text" @click="jumpDetail(item)" icon="el-icon-edit" v-if="queryAuth(item.deptIdStrs)">编辑</el-button>
                <el-button type="text" @click="handleDelete(item)" icon="el-icon-delete" v-if="queryAuth(item.deptIdStrs)">删除</el-button>
              </div>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[12, 24, 48]"
      :page-size="12"
    />
    <!-- 添加或修改图表管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="dialogWidth" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图表图片" v-if="isShow">
          <image-upload v-model="form.echartImgae"/>
        </el-form-item>
        <el-form-item label="图表名称" prop="echartName">
          <el-input v-model="form.echartName" placeholder="请输入图表名称" />
        </el-form-item>
        <el-form-item label="图表类别" prop="echartType" v-if="isShow">
          <el-select v-model="form.echartType" placeholder="请选择图表类别" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.sys_echart_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBEchartType, getBEchartType, delBEchartType, addBEchartType, updateBEchartType } from "@/api/ghxx/bEchartType";
import noImage from '@/assets/image/no-zt.png'
export default {
  name: "BEchartType",
  dicts: ['sys_echart_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图表管理表格数据
      bEchartTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        orderByColumn:"id",
        isAsc:"desc",
        guid: null,
        deptIdStrs: null,
        echartType: null,
        echartData: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        echartType: [
          { required: true, message: "图表类别不能为空", trigger: "change" }
        ],
        echartData: [
          { required: true, message: "图表内容不能为空", trigger: "blur" }
        ],
        echartName: [
          { required: true, message: "图表名称不能为空", trigger: "change" }
        ],
      },
      dialogFull:false,
      isShow:false,
      dialogWidth:'30%',
      isType:false,
      baseApi: process.env.VUE_APP_BASE_API,
      noImage: noImage,
      isAuth:true,
      cardSpan:4,
    };
  },
  created() {
    if(window.screen.width<1680){
      this.cardSpan=6;
    }else{
      this.cardSpan=4;
    }
    this.getList();
  },
  methods: {
    queryAuth(deptIdStrs){
      if(!deptIdStrs){
        return false
      }
      if(deptIdStrs.indexOf(this.$store.getters.deptId)>-1){
        return true
      }else{
        return false
      }
    },
    // 跳转详情页
    jumpDetail(row) {
      this.$router.push({
        path: '/configuration/echart/edit',
        query: {
          id: row.id,
        }
      })
    },
    /** 查询图表管理列表 */
    getList() {
      this.loading = true;
      listBEchartType(this.queryParams).then(response => {
        this.bEchartTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        guid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptIdStrs: null,
        echartType: null,
        echartData: null,
        echartImgae: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.isAuth=true;
      selection.forEach(element => {
        if(this.queryAuth(element.deptIdStrs)==false){
          this.isAuth=false;
        }
      });
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isShow=true;
      this.title = "添加图表管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if(row.id){
        this.isShow=false
        this.dialogWidth='50%'
      }else{
        this.isShow=true
        this.dialogWidth='30%'
      }
      console.log(this.dialogWidth,row);
      this.reset();
      const id = row.id || this.ids
      getBEchartType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图表管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBEchartType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBEchartType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除图表管理编号为"' + ids + '"的数据项？').then(function() {
        return delBEchartType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ghxx/bEchartType/export', {
        ...this.queryParams
      }, `图表管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.ztImage {
  width: 100%;
  display: block;
  height: 200px;
}

.ztImage:hover {
  cursor: pointer;
  transform: scale(1.1);
  transition: transform 0.8s ease-in-out
}
.device-name {
    background-color: #5479f7;
    display: inline-block;
    color: #fff;
    text-align: center;
    height: 25px;
    line-height: 15px;
    font-size: 12px;
    padding: 4px 6px;
    position: fixed;
    margin: -5px;
    z-index: 2;
}
.bottom {
  margin-top: 13px;
  line-height: 12px;
  text-align: right;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>